package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.order.SundayInStoreOrderMapper;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mysql.*;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.core.service.mysql.front.SundayInStoreOrderSKUService;
import org.itboys.core.util.NumberUtil;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/23
 * @Description：入库单，用户退货、仓库补货时调用的业务逻辑
 */
@Service
public class SundayInStoreOrderService extends BaseMySqlService<SundayInStoreOrder>{
    private static final String PREFIX="IS";
    @Autowired
    private SundayStoreAddService storeAddService;
    @Autowired
    private SundayInStoreOrderSKUService inStoreOrderSKUService;
    @Autowired
    private SundayInStoreOrderMapper inStoreOrderMapper;
    @Autowired
    private SundayStoreService storeService;
    @Autowired
    private SundaySkuService skuService;
    @Autowired
    private SundayProductService productService;

    /**
     * 生成入库单同时生成增量表
     * @param inStoreOrder
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(SundayInStoreOrder inStoreOrder, List<SundayInStoreOrderSKU> skus){
        //保存入库单
        inStoreOrder.setNumber( NumberUtil.generateNumber(PREFIX));
        inStoreOrderMapper.insert(inStoreOrder);
        for (SundayInStoreOrderSKU inStoreOrderSKU:skus
             ) {
            inStoreOrderSKU.setOrderId(inStoreOrder.getId());
        }
        inStoreOrderSKUService.saveAll(skus);
        //生成增量表
        List<SundayStoreAdd> storeAdds=new ArrayList<>();
            for (SundayInStoreOrderSKU orderSKU:skus
                 ) {
                SundayStoreAdd storeAdd=new SundayStoreAdd();
                storeAdd.setOrderId(inStoreOrder.getId());
                storeAdd.setOrderNumber(inStoreOrder.getNumber());
                storeAdd.setNum(orderSKU.getNum());
                storeAdd.setSKUId(orderSKU.getSKUId());
                storeAdd.setStoreId(inStoreOrder.getPreStoreId());
                storeAdd.setOrderType(SundayCommonConstants.ORDER_TYPE_IN_STORE);
                storeAdds.add(storeAdd);
            }
            storeAddService.save(storeAdds,inStoreOrder.getPreStoreId());
    }

    public SundayInStoreOrder findOne(Long id){
        SundayInStoreOrder order = inStoreOrderMapper.find(id);
        List<Long> ids=new ArrayList<>();
        ids.add(order.getCenterStoreId());
        ids.add(order.getPreStoreId());
        Map<String,Object> param=new HashMap();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in",ids);
        List<SundayStore> stores = storeService.list(param);
        for (SundayStore store:stores
                ) {
            if(order.getCenterStoreId().equals(store.getId())){
                order.setCenterStoreName(store.getName());
            }
            if(order.getPreStoreId().equals(store.getId())){
                order.setPreStoreName(store.getName());
            }
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("orderId",order.getId());
        List<SundayInStoreOrderSKU> skuOrders = inStoreOrderSKUService.select(param);
        ids.clear();
        for (SundayInStoreOrderSKU skuOrder:skuOrders
                ) {
            ids.add(skuOrder.getSKUId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("ids",ids);
        List<SundaySku> skus = skuService.select(param);
        ids.clear();
        for (SundaySku sku:skus
                ) {
            ids.add(sku.getProductId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("ids",ids);
        List<SundayProduct> products = productService.select(param);
        for (SundayInStoreOrderSKU skuOrder:skuOrders) {
            for (SundaySku sku:skus
                    ) {
                if(skuOrder.getSKUId().equals(sku.getId())){
                    for (SundayProduct product :products) {
                        if(product.getId().equals(sku.getProductId())){
                            skuOrder.setProductName(product.getName()+"("+sku.getNumber()+")");
                            break;
                        }
                    }
                    break;
                }
            }
        }
        order.setSkus(skuOrders);

        return order;
    }
    public PageResult<SundayInStoreOrder> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayInStoreOrder> result = super.selectPage(param);
        List<SundayInStoreOrder> orders=result.getData();
        List<Long> ids=new ArrayList<>();
        for (SundayInStoreOrder order:orders
                ) {
            ids.add(order.getPreStoreId());
            ids.add(order.getCenterStoreId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in", ids);
        List<SundayStore> stores = storeService.list(param);
        for (SundayInStoreOrder order:orders
                ) {
            for (SundayStore store:stores
                    ) {
                if(order.getCenterStoreId().equals(store.getId())){
                    order.setCenterStoreName(store.getName());
                }
                if(order.getPreStoreId().equals(store.getId())){
                    order.setPreStoreName(store.getName());
                }
            }
        }
        result.setData(orders);
        return result;
    }

}
